package com.banciyuan.bcywebview.biz.main.newdiscover.index.discoverholder;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.base.view.textview.EllipsezeTextView;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import de.greenrobot.daoexample.model.Timeline;

/**
 * Created by squall on 2017/3/22.
 */

public class GroupPostDiscoverHolder extends BaseDiscoverHolder {

    private TextView teamName;
    private LinearLayout teamNameLayout;
    private ImageView[] content;
    private EllipsezeTextView intro;
    private TextView pagenum;
    private RelativeLayout imagePics;
    private View viewDailyMark;

    public GroupPostDiscoverHolder(View itemView) {
        super(itemView);
        content = new ImageView[]{(ImageView) itemView.findViewById(R.id.daily_content_img_one),
                (ImageView) itemView.findViewById(R.id.daily_content_img_two),
                (ImageView) itemView.findViewById(R.id.daily_content_img_three)};
        teamName = (TextView) itemView.findViewById(R.id.daily_teamname_tv);
        teamNameLayout = (LinearLayout) itemView.findViewById(R.id.daily_teamname_rly);
        intro = (EllipsezeTextView) itemView.findViewById(R.id.daily_intro_tv);
        pagenum = (TextView) itemView.findViewById(R.id.daily_pagenum_tv);
        imagePics = (RelativeLayout) itemView.findViewById(R.id.rl_daily_pics);
        viewDailyMark = itemView.findViewById(R.id.tv_daily_mark);
    }

    @Override
    public void renderData(final Timeline mItem, final Context mContext) {
        super.renderData(mItem, mContext);
        teamName.setText(StringUtil.htmlDecoder(mItem.getGname()));
        if (!TextUtils.isEmpty(mItem.getContentAddress())) {
            String[] addressList = mItem.getContentAddress().split(" ");
            if (addressList.length == 0) {
                imagePics.setVisibility(View.GONE);
            } else {
                imagePics.setVisibility(View.VISIBLE);
                for (int i = 0; i < content.length; i++) {
                    if (i < addressList.length) {
                        content[i].setVisibility(View.VISIBLE);
                        ImageLoader.getInstance().displayImage(addressList[i], content[i], ImageOptions.options);
                    } else {
                        content[i].setVisibility(View.INVISIBLE);
                    }
                }
            }
        } else {
            imagePics.setVisibility(View.GONE);
        }
        String pic_num = mItem.getPic_num();
        if (!TextUtils.isEmpty(pic_num)) {
            if (Integer.parseInt(pic_num) > 3) {
                pagenum.setVisibility(View.VISIBLE);
            } else {
                pagenum.setVisibility(View.GONE);
            }
        } else {
            pagenum.setVisibility(View.GONE);
        }
        setRecommendIntro(mContext, intro, mItem, mItem.getIntro(), mContext.getString(R.string.post_topic));
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DetailDataHelper.gotoGroupDetail(mContext, mItem.getGid(), mItem.getPost_id(), false);
            }
        });
        intro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DetailDataHelper.gotoGroupDetail(mContext, mItem.getGid(), mItem.getPost_id(), false);
            }
        });
    }

    private void setRecommendIntro(Context mContext, EllipsezeTextView tvIntro, Timeline mItem, String intro, String defaultIntro) {
        String finalIntro = TextUtils.isEmpty(intro) ? defaultIntro : intro;
        finalIntro = String.format(mContext.getResources().getString(R.string.card_recommended_user_format), mItem.getOUName(), finalIntro);
        tvIntro.setText(finalIntro, mItem.getOUName(), mItem.getOuid(), true);
        tvIntro.setVisibility(View.VISIBLE);
    }
}
